var shoutListen = null;
var konami = new Konami();
konami.load();

(function($) {
	  $.fn.overlay = function()
	  {	
	      if($("#overlay")) 
	      { 
	    	  $("#overlay").remove(); 
	      }
	      
	      $("body").append("<p id='overlay'><a href='javascript:void(0)' class='closeit'>X</a></p>");

	     var filepath = this.rel;
	      
	      $("<div id='mario'><iframe src='http://www.paradize.org/mario/index.html' height='600' width='640'></iframe></div>").appendTo('#overlay').ready(function() 
	      {
		      
	    	  var wide = ($(window).width() / 2) - (640 / 2);
	    	  var high = ($(window).height() / 2) - (600 / 2);			
	    	  var scrollTop = $(window).scrollTop();
	    	  
	    	  $("#overlay").css({
	    		  top: "0px",
	    		  left: wide + "px",
	    		  display: ''
	          }).fadeIn();
	       });
	    
	     $(document).on("click", "a.closeit", function()
	     {
	    	 $("#overlay").remove();
	     });
	  };
})(jQuery);

function refreshShout() {
	$.get(BASE_URL+"/shoutbox.html", {  },
		function(data){
			$("#shoutbox-list").html(data);
			if($("#shout-scroll").is(":checked")) {
				$("#shoutbox-list").scrollTop(parseInt($("#shoutbox-list").prop('scrollHeight')));
			}
		}, "html"
	);
}

function refreshNewsComment() {
	$.get(BASE_URL+"/news-comments.html", { id: $("#news-comment").data('id') },
		function(data){
			$("#comment-list").html(data);
		}, "html"
	);
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function normalize(value) {
	permalien = value.replace(/[àâä]/gi,"a");
	permalien = permalien.replace(/[éèêë]/gi,"e");
	permalien = permalien.replace(/[îï]/gi,"i");
	permalien = permalien.replace(/[ôö]/gi,"o");
	permalien = permalien.replace(/[ùûü]/gi,"u");
	permalien = permalien.replace(/[ç]/gi,"c");
	permalien = permalien.replace(/[/]/gi,"-");
	permalien = permalien.replace(/[()!?,;*$#€%+.|&><=@~©™]/gi,"");
	permalien = permalien.replace(/[-]/gi,"");
	permalien = permalien.replace(/[:]/gi,"");
	permalien = permalien.replace('^',"");
	permalien = permalien.replace('{',"");
	permalien = permalien.replace('}',"");
	permalien = permalien.replace("'","-");
	permalien = permalien.replace('"',"");
	permalien = permalien.replace('[',"");
	permalien = permalien.replace(']',"");
	permalien = permalien.replace(/ /gi,"-");
	permalien = permalien.toLowerCase();
	permalien = trim(permalien,'-');
	return permalien;
}

$(document).ready(function() {

	$('#encyclopedie-article h3').each(function(){
		$(this).attr('id',normalize($(this).text()));
		$("#summary").append("<li><a href='#"+normalize($(this).text())+"'>"+$(this).text()+"</a></li>");
	});


	$.ajaxSetup({
		headers: {
			'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
		}
	});

	$("#shoutbox-list").scrollTop(parseInt($("#shoutbox-list").prop('scrollHeight')));

	$("#shout-it").click(function() {
		if($("#shout").val().length > 0) {
			if(shoutListen != null) {
				shoutListen.abort();
			}
			shoutListen = $.post(BASE_URL+"/shoutbox.html", { message: $("#shout").val()},
			function(data){
				$("#shout").val("");
				refreshShout();
			});
		} 
	});

	$("#shout").keypress(function(event) {
		if (event.which == "13" && $(this).val().length > 0) {
			if(shoutListen != null) {
				shoutListen.abort();
			}
			shoutListen = $.post(BASE_URL+"/shoutbox.html", { message: $("#shout").val()},
			function(data) {
				$("#shout").val("");
				refreshShout();
			});
		} 
	});

	$("#news-comment").click(function() {
		if($("#comment").val().length > 0) {
			$.post(BASE_URL+"/news-comments.html", { id: $(this).data('id'), message: $("#comment").val()},
			function(data){
				$("#comment").val("");
				refreshNewsComment();
			});
		}
	});

	if($("#news-comment").length > 0) {
		setInterval("refreshNewsComment()", 10000);
	}

	setInterval("refreshShout()", 5000);

	if($("#edit-clan-bg").length > 0) {
		$("#edit-clan-bg").click(function() {
			$("#clan-bg-content").summernote();
			$("#edit-clan-bg").hide();
			$("#save-clan-bg").show();
		});
	}

	$(".left-toggle").sidr({ name:'left-content'});


	$(window).touchwipe({
		wipeLeft: function() {
		  $.sidr('close', 'left-content');
		},
		wipeRight: function() {
		  $.sidr('open', 'left-content');
		},
		preventDefaultEvents: false
	});
});